package com.nf.health.app.service;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;

import com.nf.health.app.utils.LogUtil;
import com.nf.health.app.utils.PreferenceHelper;

/**
 * 利用系统的notification监听,得到包名来判断事件的发生
 */
@SuppressLint("NewApi")
public class NotificationMonitor extends NotificationListenerService {
	private static final String TAG = NotificationMonitor.class.getSimpleName();
	public String getCalendarPackageName() {
		return calendarPackageName;
	}
	String qqPackageName = "com.tencent.mobileqq";
	String wechatPackageName = "com.tencent.mm";
	String calendarPackageName = "com.android.calendar";
	String emailPackageName = "com.android.email";
	@Override  
    public void onNotificationPosted(StatusBarNotification sbn) {  
		LogUtil.i(TAG, sbn.getPackageName());
		if(qqPackageName.equals(sbn.getPackageName())||wechatPackageName.equals(sbn.getPackageName())){
			if(PreferenceHelper.getBoolean(PreferenceHelper.isSocialON, false)){
				//发送广播，通知手环(qq,微信)消息
				Intent intent = new Intent();
				intent.setAction(MyMsgReceiver.ACTION_NLS_CONTROL);
				intent.putExtra("type", PreferenceHelper.isSocialON);
				sendBroadcast(intent);
			}
		}
		if(calendarPackageName.equals(sbn.getPackageName())){
			if(PreferenceHelper.getBoolean(PreferenceHelper.iscalendarON, false)){
				//发送广播，通知手环日历消息
				Intent intent = new Intent();
				intent.setAction(MyMsgReceiver.ACTION_NLS_CONTROL);
				intent.putExtra("type", PreferenceHelper.iscalendarON);
				sendBroadcast(intent);
			}
		}
		if(emailPackageName.equals(sbn.getPackageName())){
			if(PreferenceHelper.getBoolean(PreferenceHelper.isEmailON, false)){
				//发送广播，通知手环邮件消息
				Intent intent = new Intent();
				intent.setAction(MyMsgReceiver.ACTION_NLS_CONTROL);
				intent.putExtra("type", PreferenceHelper.isEmailON);
				sendBroadcast(intent);
			}
		}
    }  

    @Override  
    public void onNotificationRemoved(StatusBarNotification sbn) {  
    	LogUtil.i(TAG, sbn.getPackageName());
    }
}
